package com.folderit.apps.rooms;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by César on 19/12/2014.
 */
public class RoomsAdapter extends CursorAdapter {
    private static final int VIEW_TYPE_COUNT = 1;

    /**
     * Cache of the children views for a room list item
     */
    public static class ViewHolder {
        public final ImageView iconView;
        public final TextView nameView;
        public final TextView descriptionView;
        public final TextView statusView;

        public ViewHolder(View view) {
            iconView = (ImageView) view.findViewById(R.id.list_item_icon);
            nameView = (TextView) view.findViewById(R.id.list_item_room_name);
            descriptionView = (TextView) view.findViewById(R.id.list_item_room_description);
            statusView = (TextView) view.findViewById(R.id.list_item_room_status);
        }
    }

    public RoomsAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item_room, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        ViewHolder viewHolder = (ViewHolder) view.getTag();

        // Read name from cursor
        String roomName = cursor.getString(RoomsFragment.COL_ROOM_NAME);
        viewHolder.nameView.setText(roomName);

        // Read description
        String description = cursor.getString(RoomsFragment.COL_ROOM_DESCRIPTION);
        viewHolder.descriptionView.setText(description);

        // Set icon
        int colorId = cursor.getInt(RoomsFragment.COL_ROOM_COLOR_ID);
        viewHolder.iconView.setImageResource(Utility.getRoomIconIdByColorId(colorId));

        // TODO: Set room current status (free or used)
        // To do that I suppose i need a new adapter
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }
}
